<template>
  <view class="container">
    <scroll-view 
      class="scroll-container" 
      scroll-y 
      :show-scrollbar="false"
    >
      <!-- 顶部大图 -->
      <view class="hero-section">
        <image 
          class="hero-image" 
          :src="mainImage" 
          mode="aspectFill" 
          @error="handleImageError"
          @load="handleImageLoad"
          :style="{ opacity: imageLoaded ? 1 : 0 }"
        />
        <view v-if="!imageLoaded" class="image-loading">
          <van-loading type="spinner" color="#ffffff" />
        </view>
        <view class="hero-overlay">
          <text class="hero-title">蓝月谷</text>
          <text class="hero-subtitle">BLUE MOON VALLEY</text>
        </view>
      </view>

      <!-- 场景描述 -->
      <view class="scene-section">
        <view class="scene-content">
          <text class="scene-text">蓝月谷，如诗如画的仙境。碧蓝的湖水倒映着雪山，清澈见底，仿佛一块巨大的蓝宝石镶嵌在群山之间。这里的水来自玉龙雪山的融雪，在阳光的照射下呈现出梦幻般的蓝色。</text>
        </view>
      </view>

      <!-- 照片展示 -->
      <view class="photo-section">
        <view class="photo-grid">
          <view class="photo-item" v-for="(item, index) in galleryList" :key="index">
            <image 
              :src="item.image" 
              mode="aspectFill" 
              @error="handleImageError"
            />
            <view class="photo-info">
              <text class="photo-title">{{ item.title }}</text>
              <text class="photo-time">{{ item.time }}</text>
            </view>
          </view>
        </view>
      </view>
      <van-back-top  bottom="20vh" />
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { fetchBlueMoonImages } from '@/services/imageService';

const galleryList = ref([]);
const mainImage = ref('');
const imageLoaded = ref(false);

// 获取蓝月谷的图片
const fetchBlueMoonValleyImages = async () => {
  try {
    // 并行获取横版和竖版图片
    const [horizontalRes, verticalRes] = await Promise.all([
      request({
        url: '/api/profilepic/GetProfilesAsync?_type=71', // 蓝月谷横版
        method: 'get'
      }),
      request({
        url: '/api/profilepic/GetProfilesAsync?_type=72', // 蓝月谷竖版
        method: 'get'
      })
    ]);

    const allImages = [];
    const imageUrls = new Set(); // 用于去重

    // 处理横版图片
    if (horizontalRes.status && horizontalRes.data) {
      horizontalRes.data.forEach(item => {
        let imageUrl = item.picUrl;
        if (imageUrl && !imageUrl.startsWith('http')) {
          imageUrl = `http://${imageUrl}`;
        }
        if (!imageUrls.has(imageUrl)) {
          imageUrls.add(imageUrl);
          allImages.push({
            image: imageUrl,
            title: "蓝月谷",
            description: "碧水蓝天，如诗如画"
          });
        }
      });
    }

    // 处理竖版图片
    if (verticalRes.status && verticalRes.data) {
      verticalRes.data.forEach(item => {
        let imageUrl = item.picUrl;
        if (imageUrl && !imageUrl.startsWith('http')) {
          imageUrl = `http://${imageUrl}`;
        }
        if (!imageUrls.has(imageUrl)) {
          imageUrls.add(imageUrl);
          allImages.push({
            image: imageUrl,
            title: "蓝月谷",
            description: "碧水蓝天，如诗如画"
          });
        }
      });
    }

    // 设置主图（使用第一张横版图片）
    if (horizontalRes.status && horizontalRes.data && horizontalRes.data.length > 0) {
      let mainImageUrl = horizontalRes.data[0].picUrl;
      if (mainImageUrl && !mainImageUrl.startsWith('http')) {
        mainImageUrl = `http://${mainImageUrl}`;
      }
      mainImage.value = mainImageUrl;
    }

    // 设置照片列表（最多6张）
    galleryList.value = allImages.slice(0, 6);
  } catch (error) {
    console.error('Failed to fetch Blue Moon Valley images:', error);
    uni.showToast({
      title: '获取图片失败',
      icon: 'error'
    });
  }
};

// 处理图片加载错误
const handleImageError = (e) => {
  console.error('Image load error:', e);
  if (e.target) {
    e.target.src = 'https://img01.yzcdn.cn/vant/apple-1.jpg';
  }
};

// 处理图片加载成功
const handleImageLoad = () => {
  imageLoaded.value = true;
};

onMounted(async () => {
  try {
    galleryList.value = await fetchBlueMoonImages();
  } catch (error) {
    console.error('Failed to initialize:', error);
    uni.showToast({
      title: '加载失败，请重试',
      icon: 'error'
    });
  }
});
</script>

<style lang="scss" scoped>
page {
  height: 100%;
  background-color: #f5f5f5;
}

.container {
  height: 100%;
  width: 100%;
}

.scroll-container {
  height: 100%;
  width: 100%;
}

.hero-section {
  position: relative;
  height: 100vh;
  width: 100%;
  
  .hero-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: opacity 0.3s ease, transform 0.5s ease;
    animation: zoomIn 1.5s ease-out;
  }
  
  .hero-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 40px 20px;
    background: linear-gradient(to top, rgba(245, 245, 245, 0.9), transparent);
    animation: fadeInUp 1s ease-out;
    
    .hero-title {
      font-size: 48px;
      font-weight: bold;
      color: #333333;
      display: block;
      margin-bottom: 8px;
      text-shadow: 0 2px 4px rgba(0,0,0,0.1);
      font-family: 'NSimSun', '新宋体', serif;
      letter-spacing: 4px;
      animation: slideInLeft 1s ease-out;
    }
    
    .hero-subtitle {
      font-size: 24px;
      color: #666666;
      display: block;
      letter-spacing: 2px;
      font-family: 'STXingkai', '华文行楷', serif;
      animation: slideInRight 1s ease-out;
    }
  }
}

.scene-section {
  padding: 60px 20px;
  background-color: #f5f5f5;
  animation: fadeIn 1s ease-out;
  
  .scene-content {
    max-width: 600px;
    margin: 0 auto;
    
    .scene-text {
      font-size: 18px;
      line-height: 1.8;
      color: #333333;
      text-align: center;
      letter-spacing: 1px;
      font-family: 'STXingkai', '华文行楷', serif;
      animation: fadeInUp 1s ease-out;
    }
  }
}

.photo-section {
  padding: 0 20px 40px;
  background-color: #f5f5f5;
  
  .photo-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    max-width: 1200px;
    margin: 0 auto;
  }
  
  .photo-item {
    position: relative;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
    animation: fadeInUp 0.8s ease-out;
    animation-fill-mode: both;
    background-color: #ffffff;
    
    @for $i from 1 through 6 {
      &:nth-child(#{$i}) {
        animation-delay: #{$i * 0.1}s;
      }
    }
    
    &:hover {
      transform: translateY(-5px);
      
      image {
        transform: scale(1.05);
      }
      
      .photo-info {
        background: linear-gradient(to top, rgba(0,0,0,0.7), rgba(0,0,0,0.4));
      }
    }
    
    image {
      width: 100%;
      height: 240px;
      object-fit: cover;
      transition: transform 0.3s ease;
    }
    
    .photo-info {
      position: absolute;
      bottom: 0;
      left: 0;
      right: 0;
      padding: 20px;
      background: linear-gradient(to top, rgba(0,0,0,0.6), transparent);
      transition: background 0.3s ease;
      
      .photo-title {
        font-size: 18px;
        color: #ffffff;
        display: block;
        margin-bottom: 4px;
        font-family: 'NSimSun', '新宋体', serif;
        letter-spacing: 1px;
      }
      
      .photo-time {
        font-size: 14px;
        color: rgba(255,255,255,0.9);
        display: block;
        font-family: 'STXingkai', '华文行楷', serif;
      }
    }
  }
}

@keyframes zoomIn {
  from {
    transform: scale(1.1);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideInLeft {
  from {
    opacity: 0;
    transform: translateX(-30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}

@keyframes slideInRight {
  from {
    opacity: 0;
    transform: translateX(30px);
  }
  to {
    opacity: 1;
    transform: translateX(0);
  }
}
</style> 